<?php
namespace app\admin\controller;

use think\facade\View;
use app\Request;
use app\repositories\contracts\ConfigInterface;

class Config extends ViewBaseController
{
    public function index(ConfigInterface $config)
    {
        $configList = $config->getConfigList();
        return View::fetch('',['configList'=>$configList]);
    }

    public function getConfigListJson(Request $request,ConfigInterface $config)
    {
        $page = $request->param('page');
        $limit = $request->param('limit');
        return layuiJson(0,'配置列表获取成功',$config->getConfigListPage($page,$limit,'id','asc'),$config->getConfigListCount());
    }

    /***
     * @param ConfigInterface $config
     * @return \think\response\Json
     * @Author Vee7
     * @Date 2021/12/15
     * @Time 9:21
     * @Description 删除配置接口
     */
    public function del(Request $request,ConfigInterface $config)
    {
        $idArray = $request->param('idArray');
        $bool = $config->delConfigList($idArray);
        if(!$bool)
        {
            $result = layuiJson(1,'删除失败');
            return $result;
        }
        return layuiJson(0,'删除成功',$idArray);
    }

    /***
     * @param Request $request
     * @param ConfigInterface $config
     * @return \think\response\Json
     * @Author Vee7
     * @Date 2021/12/15
     * @Time 10:40
     * @Description 修改指定字段接口
     */
    public function editList(Request $request,ConfigInterface $config)
    {
        $editArray = $request->param();
        $result = $config->editConfigList($editArray);
        if(!$result)
        {
            $result = layuiJson(1,'修改失败');
            return $result;
        }
        return layuiJson(0,'修改成功',$result);
    }

    /***
     * @Author Vee7
     * @Date 2021/12/15
     * @Time 13:46
     * @Description 添加配置
     */
    public function add(Request $request,ConfigInterface $config)
    {
        if(!$request->isPost()){
            $allConfigList = $config->getConfigList();
            return View::fetch('',['allConfigList'=>$allConfigList]);
        }

        $addArray = $request->param();
        $result = $config->addConfigList($addArray);
        if(!$result){
            return layuiJson(1,'添加失败,用户名可能已存在',$result);
        }

        return layuiJson(0,'添加成功',$result);
    }
}
